﻿package com.urbansquall.display {
import flash.display.*;
import flash.geom.Matrix;
import flash.geom.Rectangle;

public class Scale9Data
{
	internal var m_topLeft : BitmapData;
	internal var m_topMiddle : BitmapData;
	internal var m_topRight : BitmapData;
	
	internal var m_middleLeft : BitmapData;
	internal var m_middleMiddle : BitmapData;
	internal var m_middleRight : BitmapData;
	
	internal var m_bottomLeft : BitmapData;
	internal var m_bottomMiddle : BitmapData;
	internal var m_bottomRight : BitmapData;
	
	internal var m_scale9Rect : Rectangle;
	
	public function Scale9Data( a_source : DisplayObject, a_scale9Rect : Rectangle )
	{
		m_scale9Rect = a_scale9Rect;
		
		var bounds : Rectangle = a_source.getBounds( a_source );
		
		var left : Number= m_scale9Rect.x;
		var right : Number = m_scale9Rect.x + m_scale9Rect.width;
		
		var top : Number = m_scale9Rect.y;
		var bottom : Number = m_scale9Rect.y + m_scale9Rect.height;
		
		m_topLeft = new BitmapData( left, top, true, 0 );
		m_topMiddle = new BitmapData( m_scale9Rect.width, top, true, 0 );
		m_topRight = new BitmapData( bounds.width - right, top, true, 0 );
		
		m_middleLeft = new BitmapData( left, m_scale9Rect.height, true, 0 );
		m_middleMiddle = new BitmapData( m_scale9Rect.width, m_scale9Rect.height, true, 0 );
		m_middleRight = new BitmapData( bounds.width - right, m_scale9Rect.height, true, 0 );
		
		m_bottomLeft = new BitmapData( left, bounds.height - bottom, true, 0 );
		m_bottomMiddle = new BitmapData( m_scale9Rect.width, bounds.height - bottom, true, 0 );
		m_bottomRight = new BitmapData( bounds.width - right, bounds.height - bottom, true, 0 );
		
		carvePiece( m_topLeft, 0, 0, a_source );
		carvePiece( m_topMiddle, left, 0, a_source );
		carvePiece( m_topRight, right, 0, a_source );
		
		carvePiece( m_middleLeft, 0, top, a_source );
		carvePiece( m_middleMiddle, left, top, a_source );
		carvePiece( m_middleRight, right, top, a_source );

		carvePiece( m_bottomLeft, 0, bottom, a_source );
		carvePiece( m_bottomMiddle, left, bottom, a_source );
		carvePiece( m_bottomRight, right, bottom, a_source );
	}
	
	private function carvePiece( a_piece : BitmapData, a_x : Number, a_y : Number, a_source : DisplayObject ) : void
	{
		var bounds : Rectangle = a_source.getBounds( a_source );
		var matrix : Matrix = new Matrix();
		matrix.translate( -bounds.x, -bounds.y );
		matrix.translate( -a_x, -a_y );
		a_piece.draw( a_source, matrix, null, null, new Rectangle( 0, 0, a_piece.width, a_piece.height )  );
	}
	
	public function dispose() : void
	{
		m_topLeft.dispose();
		m_topMiddle.dispose();
		m_topRight.dispose();

		m_middleLeft.dispose();
		m_middleMiddle.dispose();
		m_middleRight.dispose();
		
		m_bottomLeft.dispose();
		m_bottomMiddle.dispose();
		m_bottomRight.dispose();
		
		m_topLeft = null;
		m_topMiddle = null;
		m_topRight = null;

		m_middleLeft = null;
		m_middleMiddle = null;
		m_middleRight = null;
		
		m_bottomLeft = null;
		m_bottomMiddle = null;
		m_bottomRight = null;
	}
} // c
} // p